﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SceneFrame
{
    /// <summary>
    /// 默认的过滤器
    /// </summary>
    public class FilterTrigger : IFilter
    {
        private Dictionary<string, string> _Mapping = null;

        public FilterTrigger() { }

        public FilterTrigger(Dictionary<string, string> dic)
        {
            _Mapping = new Dictionary<string, string>(dic);
        }

        public FilterTrigger(IEnumerable<KeyValuePair<string, string>> filters)
        {
            _Mapping = filters?.ToDictionary(k => k.Key, v => v.Value);
        }

        public FilterTrigger(params KeyValuePair<string, string>[] filters)
            : this(new List<KeyValuePair<string, string>>(filters)) { }

        /// <summary>
        /// 拦截触发器
        /// </summary>
        /// <param name="trigger"></param>
        /// <returns></returns>
        public override bool Filter(Trigger trigger)
        {
            if ((_Mapping?.Count ?? 0) == 0)
                return true;

            foreach (var item in _Mapping)
            {
                if (trigger.GetData(item.Key) != item.Value)
                    return false;
            }

            return true;
        }
    }
}
